﻿using Ec_System.Models.Web;
using Ec_System.Service.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Ec_System.Bll.Web
{
    public class ItemOrderManager
    {
        ItemOrderService itemOrderService = new ItemOrderService();

        /// <summary>
        /// 根据LoginID查询前5条订单
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ItemOrderData> GetTop5OrderByLoginID(string loginID)
        {
            var orderList = itemOrderService.GetTop5OrderByLoginID(loginID);
            List<ItemOrderData> orders = new List<ItemOrderData>();
            orders.AddRange(orderList);

            //去除重复的OrderID
            for(int i = 0; i < orders.Count - 1; i++)
            {
                for(int j = orders.Count - 1; j > i; j--)
                {
                    if(orders[i] == orders[j])
                    {
                        orders.RemoveAt(j);
                    }
                }
            }

           foreach(var order in orders)
            {
                foreach(var item in orderList)
                {
                    if(order.OrderId == item.OrderId)
                    {
                        order.OrderList.Add(item);
                    }
                }
            }

            return orders;
        }

        // <summary>
        /// 根据LoginID查询指定的订单(带分页)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ItemOrderResponseData GetOrderListWithPageByLoginID(ItemOrderRequestData request)
        {
            //获取到指定的订单
            var orderList = itemOrderService.GetOrderListWithPageByLoginID(request);
            //构建明细查询的xml,--订单号
            var root = new XElement("Orders");
            root.Add(orderList.Result.Select(o => new XElement("Order", o.OrderId)));
            //查询明细
            var dateilsList = itemOrderService.GetOrderDetailByOrderID(new XDocument(root).ToString());

            //把查询到的子订单添加到对应的父订单
            foreach(var order in orderList.Result)
            {
                foreach(var dateil in dateilsList)
                {
                    if(order.OrderId == dateil.OrderId)
                    {
                        order.ItemDatailsList.Add(dateil);
                    }
                }
            }

            return orderList;
        }

        /// <summary>
        /// 退货
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="orderDetailId"></param>
        /// <param name="vendorNumber"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public int ReturnOrder(string orderId, string orderDetailId, string vendorNumber, string reason)
        {
            return itemOrderService.ReturnOrder(orderId, orderDetailId, vendorNumber, reason);
        }

        /// <summary>
        /// 根据OrderID和ProductID获取评价订单信息, 用于评价页面
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public ItemOrderData GetOrderInfoByOrderIDAndProductID(string orderID, string productID)
        {
            var model = itemOrderService.GetOrderInfoByOrderID(orderID);
            model.ItemDatailsList.Add(itemOrderService.GetOrderDetailByOrderIDAndPID(orderID, productID));

            return model;
        }
    }
}
